﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;

namespace Wpf.Telemetry.TestApp
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        Monitoring monitoring;

        public App()
        {
            this.Exit += new ExitEventHandler(App_Exit);
            ConfigureMonitoring();
            this.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);
        }

        private void ConfigureMonitoring()
        {
            ITelemetryReporter reporter = new WebServiceTelemetryReporter(); // if you get an exception here you probably don't have the WCF config in place in your client app
            monitoring = new Monitoring(1 /* in practice you probably want to make this a bit higher */, reporter);
        }

        void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            if (monitoring != null)
            {
                monitoring.SendErrorReport(e.Exception, "Prompt the user for some kind of description", "person@example.com");
            }
        }

        void App_Exit(object sender, ExitEventArgs e)
        {
            monitoring.ReportFeatureUsage();
        }

        public Monitoring Monitoring
        {
            get { return monitoring; }
        }

        public static App CurrentInstance
        {
            get { return App.Current as App; }
        }
    }
}
